#!/usr/bin/env python
#coding:utf-8
import sys
import re 
import urllib2
class ip138:
    def __init__(self,ip):
        self.ip=ip
    def __open(self):
        return urllib2.urlopen('http://ip138.com/ips138.asp?ip='+self.ip + '&action=2')
    def __recompile(self):
        return re.compile(r'.*<li>(.*)</li><li>(.*)</li>.*')
    def get(self):
        p = self.__recompile()
        lines = self.__open()
#        print lines
        for line in lines:
            if '<ul class="ul1">' in line:
                return p.sub(r'\1\n\2',line).decode('gbk')
ip=sys.argv[1]
if __name__=='__main__':
    m = ip138(ip)
    print "查询ip:%s\n%s" %(ip,m.get().encode('utf-8')),
